# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

# 哈希

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        p = headA
        d = set()
        while p:
            d.add(p)
            p = p.next
        p = headB
        while p:
            if p in d:
                return p
            p = p.next